///////////////////////////////////////////////////////////////////////////////
//
//  Copyright (C) 2007-2009 ILOG, S.A.
//  All Rights Reserved.
//  The following is ILOG Source Code.  No modifications may be made to the  
//  ILOG Source Code.  Any usage of the ILOG Source Code is subject to 
//  the terms and conditions of the ILOG End User License Agreement   
//  applicable to this ILOG software product.
//
///////////////////////////////////////////////////////////////////////////////

package ilog.gauges.circular.renderers
{
  import flash.display.Graphics;
  import flash.display.Sprite;
  import flash.geom.Rectangle;
  
  import ilog.utils.CSSUtil;
  
  import mx.graphics.GradientEntry;
  import mx.graphics.IFill;
  import mx.graphics.IStroke;
  import mx.graphics.RadialGradient;
  import mx.styles.CSSStyleDeclaration;

  /**
   * A needle renderer that you can use to represent the central piece of a knob
   * in charge of representing a given value and allowing you to interact with
   * it to set the current value.
   * 
   * @see ilog.gauges.controls.simple.SimpleKnob
   */
  public class KnobRenderer extends NeedleRenderer
  {
    /**
     * Constructor.
     */
    public function KnobRenderer()
    {
      super();
    }

    /**
     *  @private
     */     
    private static var stylesInited:Boolean = initStyles(); 

    /**
     *  @private
     */ 
    private static function initStyles():Boolean
    {
      var nStyle:CSSStyleDeclaration =
        CSSUtil.createSelector("KnobRenderer");   
            
      nStyle.defaultFactory = function():void  {
        var grad:RadialGradient = new RadialGradient();
        grad.focalPointRatio = 0.5
        var ge1:GradientEntry = new GradientEntry(0xFF0000);
        var ge2:GradientEntry = new GradientEntry(0xAA0000, 1);
        grad.entries = [ge1, ge2];
        grad.angle = 180;
        this.fill = grad;
      }

      return true;
    }    
    
    /** 
     * This method is redefined to draw a knob instead of the default needle.
     *
     * @param needle The Sprite into which the needle must be drawn.
     * @param radius The total length of the needle, in pixels.
     * @param baseRadius The starting radius of the needle. 
     * @param needleWidth The width, in pixels, of the needle at the needle's base origin.
     * @param pointWidth The width, in pixels, of the needle's point origin.
     * @param pointHeight The height, in pixels, of the needle's point.
     * @param fill The fill to use to fill the needle.
     * @param stroke The stroke to use to draw the needle outline.
     */ 
    override protected function drawNeedle(needle:Sprite, radius:Number, baseRadius:Number, 
                                           needleWidth:Number, 
                                           pointWidth:Number, 
                                           pointHeight:Number, 
                                           fill:IFill, stroke:IStroke):void
    {
      var g:Graphics = needle.graphics;
      var c1:Number = 0.7;
      var c2:Number = 0.25;
      g.beginFill(0, 0);
      g.drawCircle(0, 0, radius);
      
      g.clear();
      if (stroke)
        stroke.apply(g);
      
      var lc:Rectangle = new Rectangle();
      lc.x = (c1 - c2) * radius;
      lc.y = -c2*radius;
      lc.width = c2 * radius * 2;
      lc.height = c2 * radius * 2;
      if (fill)
        fill.begin(g, lc);

      g.drawCircle(c1 * radius, 0, c2 * radius);
      if (fill)
        fill.end(g);
    }  
  }
}
